import { getYMD, getNowFormatDate, getDay, formatGMTDate, sortBy, toPercent } from '../../../assets/js/util.js'

export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            games: [],
            projects: [],
            // 事件下拉
            options5: [],
            // form输入框的胡宽度
            formLabelWidth: '95px',
            /*initWidth 的宽*/
            initWidth: '100%',
            // 添加或搜索数据
            ruleForm: {
                region: '',
                pzId: '', // id
                pzval: '', // 参数值
                pzSort: 0 // 排序
            },
            // 表格数据
            tableData: [],
            tableDataAll: [],
            // 验证ruleForm
            rules: {
                region: [
                        { type: 'number', required: true, message: '请选择参数类型', trigger: 'change' }
                    ]
                    //   ,
                    // pzval: [
                    //    {type: 'string', required: true, message: '请填写参数值', trigger: 'change' }
                    // ]
            },
            // 分页
            pageSize: 10,
            pageSizes: [10, 20, 50, 100],
            dataTotal: 0,
            currentPage: 1,

            // 上传文件显示隐藏
            uploadShow: false,
        }
    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        // 获取数据 获取工程List 以及页面展示数据
        getProjectData() {
            this.projects = [];
            this.$http.post(`${this.SRC}project/list.do`).then(
                function(res) {
                    var data = res.body;
                    if (data["code"] === 0) {
                        for (var i = 0; i < data['msg'].length; i++) {
                            var obj = {
                                label: data["msg"][i]['projectName'],
                                value: data["msg"][i]['id']
                            }
                            this.projects.push(obj);
                        }
                    } else {
                        this.errorFun(data['msg'])
                    }
                }
            )
        },
        // 选择游戏
        projectsChange(val) {
            var p = this.projects;
            for (var i = 0; i < p.length; i++) {
                if (val === p[i].value) {
                    this.ruleForm.gameName = p[i].label;
                    this.ruleForm.gameId = p[i].value;
                }
            }
        },
        // 数组转json
        getArrJson(o) {
            var arr = [];
            for (var i of o) {
                var obj = {};
                obj.value = i[0];
                obj.label = i[1];
                arr.push(obj)
            }
            return arr;
        },
        commonAjax(c, p) {
            var gameid = this.games.join(',');
            this.$http.get(`${this.SRC}gameparam/list.do?curPage=${c}&pageSize=${p}`).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.tableData = sortBy(data.msg.data, 'id');
                    this.currentPage = data.msg.curPage;
                    this.dataTotal = data.msg.totalSize;
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
        },
        // 参数类型选择
        options5Fn(o) {
            this.ruleForm.region = o;
        },
        // 分页
        handleSizeChange(val) {
            this.pageSize = val;
            this.commonAjax(this.currentPage, this.pageSize);
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.commonAjax(this.currentPage, this.pageSize);
        },
        /*设置初始值*/
        _init() {
            // 参数下拉
            this.$http.post(`${this.SRC}gameparam/paramtypes.do`).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.options5 = this.getArrJson(data.msg)
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.initWidth = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // search(currentPage,pageSize){
        //   this.commonAjax(currentPage,pageSize);
        // },

        // 上传
        uploadForm() {
            this.uploadShow = true;
        },
        uploadHide() {
            this.uploadShow = false;
            this.commonAjax(this.currentPage, this.pageSize);
        },
        // 编辑
        handleEdit(index, row) {
            var that = this;
            new Promise(function(resolve, reject) {
                that.$http.post(`${that.SRC}gameparam/modify.do?id=${row.id}&paramtype=${row.paramtype}&gameId=${row.gameId}&paramid=${row.paramid}&paramname=${row.paramname}&seq=${row.seq}`).then(function(data) {
                    if (data.body.code == 0) {
                        that.successFun(data.body['msg']);
                    } else {
                        that.errorFun(data.body['msg'])
                    }
                })
            });

        },
        // 删除用户
        handleDelete(index, row) {
            var that = this;
            var p1 = new Promise(function(resolve, reject) {
                that.$http.post(`${that.SRC}gameparam/delete.do?id=${row.id}`).then(function(data) {
                    return resolve(data.body)
                    that.successFun(data.body['msg']);
                })
            }).then(function(dataJson) {
                that.$http.post(`${that.SRC}gameparam/list.do?curPage=${that.currentPage}&pageSize=${that.pageSize}`).then(function(data) {
                    if (dataJson.code == 0 && data.body.code == 0) {
                        this.tableData = sortBy(data.body.msg.data, 'id');
                        this.currentPage = data.body.msg.curPage;
                        this.dataTotal = data.body.msg.totalSize;
                        this.successFun(dataJson['msg']);
                    } else {
                        this.errorFun(dataJson['msg']);
                    }
                })
            })
        },
        submitForm(formName) {
            var that = this;
            var gameid = this.games.join(',');
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    var p3 = new Promise(function(resolve, reject) {
                        that.$http.get(`${that.SRC}gameparam/add.do?paramtype=${that.$refs[formName].model.region}&gameId=${gameid},&paramid=${that.$refs[formName].model.pzId}&paramname=${that.$refs[formName].model.pzval}&seq=${that.$refs[formName].model.pzSort}`).then(function(data) {
                            resolve(data.body)
                        })
                    });
                    p3.then(function(dataJson) {
                        that.$http.post(`${that.SRC}gameparam/list.do?curPage=${that.currentPage}&pageSize=${that.pageSize}`).then(function(data) {
                            if (dataJson.code == 0 && data.body.code == 0) {
                                that.ruleForm.pzId = '';
                                that.ruleForm.pzval = '';
                                that.tableData = sortBy(data.body.msg.data, 'id');
                                console.log(data.body.msg)
                                that.currentPage = data.body.msg.curPage;
                                that.dataTotal = data.body.msg.totalSize;
                                that.successFun(dataJson['msg']);
                            } else {
                                that.errorFun(dataJson['msg']);
                            }
                        })
                    })

                } else {
                    return false;
                }
            });
        },
        serachForm(formName) {
            var that = this;
            this.$http.get(`${that.SRC}gameparam/list.do?curPage=${that.currentPage}&pageSize=${that.pageSize}&paramtype=${that.$refs[formName].model.region}&gameId=${that.games}&paramid=${that.$refs[formName].model.pzId}&paramname=${that.$refs[formName].model.pzval}&seq=${that.$refs[formName].model.pzSort}`).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.tableData = sortBy(data.msg.data, 'id');
                    this.currentPage = data.msg.curPage;
                    this.dataTotal = data.msg.totalSize;
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.games = [];
        }
    },
    mounted() {
        this.SRC = this.$store.getters.src3;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this._init();
        this.getProjectData();
        this.commonAjax(this.currentPage, this.pageSize);
        //console.log(localStorage.getItem("ZULONGBINAVLIST"))
    }
}